package com.shareach.http.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shareach.http.action.SysAskAction;
import com.shareach.web.basic.Answer;
import com.shareach.web.basic.Ask;
import com.shareach.web.controller.BaseController;

@Controller
@RequestMapping("/sys")
public class SysController extends BaseController{
	//private static Logger log = LoggerFactory.getLogger(DataController.class);
	@Autowired
	private SysAskAction sysAskAction;

	@RequestMapping(value = "/js.do", method = RequestMethod.POST)
	public @ResponseBody Answer<?> run(@RequestBody Ask ask,HttpServletRequest request) {
		printLog(ask);
		Answer<?> answer = sysAskAction.doService(ask, request.getRemoteAddr());
		saveCommandAction(request.getRemoteAddr(), ask, answer);
		return answer;
	}
	@RequestMapping(value = "/data.do", method = RequestMethod.POST)
	public @ResponseBody Answer<?> run(HttpServletRequest request, HttpServletResponse response) {
		Answer<?> answer = null;
		String data = this.getString(request, "data", null, false);
		String msg = "";
		if(StringUtils.isBlank(data)){
		}else{
			try{
				Ask ask = this.decodeFromBase64(data, Ask.class);
				printLog(ask);
				answer = sysAskAction.doService(ask, request.getRemoteAddr());
				saveCommandAction(request.getRemoteAddr(), ask, answer);
			}catch(Exception exp){
				msg = exp.getMessage();
				exp.printStackTrace();
			}
		}
		if(answer==null){
			answer = new Answer<Object>();
			answer.setMsg((msg==null)?"非法操作":msg);
		}
		return answer;
	}
	
	@RequestMapping(value = "/js.do", method = RequestMethod.GET)
	public @ResponseBody String runJSGet(HttpServletRequest request, HttpServletResponse response) {
		return "Invalid";
	}
	@RequestMapping(value = "/data.do", method = RequestMethod.GET)
	public @ResponseBody String runDataGet(HttpServletRequest request, HttpServletResponse response) {
		return "Invalid";
	}
}
